package com.beordie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beordie.dao.SellerDao;
import com.beordie.model.Seller;
import com.beordie.model.User;
import com.beordie.service.ISellerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description
 * @Date 2021/8/5 16:38
 * @Created 30500
 */
@Service
public class SellerServiceImpl implements ISellerService {
    @Autowired
    private SellerDao sellerDao;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<Seller> findPage(Seller condition, int pageNum, int pageSize) {
        QueryWrapper<Seller> wrapper = new QueryWrapper<>();

        if (condition.getName() != null)
            wrapper.like("name", condition.getName());
        if (condition.getAddress() != null)
            wrapper.like("address", condition.getAddress());

        PageHelper.startPage(pageNum, pageSize);
        List<Seller> sellerList = sellerDao.selectList(wrapper);
        return new PageInfo<>(sellerList);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public List<Seller> find() {
        QueryWrapper<Seller> wrapper = new QueryWrapper<>();
        List<Seller> sellerList = sellerDao.selectList(wrapper);
        return sellerList;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int add(Seller seller) {
        return sellerDao.insert(seller);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Seller findById(Integer id) {
        return sellerDao.selectById(id);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int update(Seller seller) {
        return sellerDao.updateById(seller);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int delete(Integer id) {
        return sellerDao.deleteById(id);
    }
}
